home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #5 / Amiga Plus CD - 2000 - No. 5.iso / Tools / Dev / lame_src / timestatus.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-01  |  4.4 KB  |  175 lines

  1. #include "lame.h"
  2. #include "timestatus.h"
  3. #include "util.h"
  4. #include <time.h>
  5.  
  6. #if defined(CLOCKS_PER_SEC)
  7. /* ANSI/ISO systems */
  8. # define TS_CLOCKS_PER_SEC CLOCKS_PER_SEC
  9. #elif defined(CLK_TCK)
  10. /* Non-standard systems */
  11. # define TS_CLOCKS_PER_SEC CLK_TCK
  12. #elif defined(HZ)
  13. /* Older BSD systems */
  14. # define TS_CLOCKS_PER_SEC HZ
  15. #else
  16. # error no suitable value for TS_CLOCKS_PER_SEC
  17. #endif
  18.  
  19. /*********************************************************/
  20. /* ts_real_time: real time elapsed in seconds            */
  21. /*********************************************************/
  22. FLOAT ts_real_time(long frame) {
  23.  
  24.   static time_t initial_time;
  25.   time_t current_time;
  26.  
  27.   time(¤t_time);
  28.  
  29.   if (frame==0) {
  30.     initial_time = current_time;
  31.   }
  32.  
  33.   return (FLOAT) difftime(current_time, initial_time);
  34. }
  35.  
  36. /*********************************************************/
  37. /* ts_process_time: process time elapsed in seconds      */
  38. /*********************************************************/
  39. FLOAT ts_process_time(long frame) {
  40.   static clock_t initial_time;
  41.   clock_t current_time;
  42.  
  43. #if ( defined(_MSC_VER) || defined(__BORLANDC__) ) 
  44.  
  45.   { static HANDLE hProcess;
  46.     FILETIME Ignored1, Ignored2, KernelTime, UserTime;
  47.  
  48.     if ( frame==0 ) {
  49.       hProcess = GetCurrentProcess();
  50.     }
  51.         
  52.     /* GetProcessTimes() always fails under Win9x */
  53.     if (GetProcessTimes(hProcess, &Ignored1, &Ignored2, &KernelTime, &UserTime)) {
  54.       LARGE_INTEGER Kernel = { KernelTime.dwLowDateTime, KernelTime.dwHighDateTime };
  55.       LARGE_INTEGER User = { UserTime.dwLowDateTime, UserTime.dwHighDateTime };
  56.  
  57.       current_time = (clock_t)((FLOAT)(Kernel.QuadPart + User.QuadPart) * TS_CLOCKS_PER_SEC / 10000000);
  58.     } else {
  59.       current_time = clock();
  60.     }
  61.   }
  62. #else
  63.   current_time = clock();
  64. #endif
  65.  
  66.   if (frame==0) {
  67.     initial_time = current_time;
  68.   }
  69.  
  70.   return (FLOAT)((FLOAT)(current_time - initial_time) / TS_CLOCKS_PER_SEC);
  71. }
  72.  
  73. #undef TS_CLOCKS_PER_SEC
  74.  
  75. typedef struct ts_times {
  76.   FLOAT so_far;
  77.   FLOAT estimated;
  78.   FLOAT speed;
  79.   FLOAT eta;
  80. } ts_times;
  81.  
  82. /*********************************************************/
  83. /* ts_calc_times: calculate time info (eta, speed, etc.) */
  84. /*********************************************************/
  85. void ts_calc_times(ts_times *tstime, int samp_rate, long frame, long frames,int framesize)
  86. {
  87.   if (frame > 0) {
  88.     tstime->estimated = tstime->so_far * frames / frame;
  89.     if (samp_rate * tstime->estimated > 0) {
  90.       tstime->speed = frames * framesize / (samp_rate * tstime->estimated);
  91.     } else {
  92.       tstime->speed = 0;
  93.     }
  94.     tstime->eta = tstime->estimated - tstime->so_far;
  95.   } else {
  96.     tstime->estimated = 0;
  97.     tstime->speed = 0;
  98.     tstime->eta = 0;
  99.   }
  100. }
  101.  
  102. /*********************************************************/
  103. /* timestatus: display encoding process time information */
  104. /*********************************************************/
  105. void timestatus(int samp_rate,long frameNum,long totalframes,int framesize)
  106. {
  107.   ts_times real_time, process_time;
  108.   int percent;
  109.  
  110.   real_time.so_far = ts_real_time(frameNum);
  111.   process_time.so_far = ts_process_time(frameNum);
  112.  
  113.   if (frameNum == 0) {
  114.     fprintf(stderr, "    Frame          |  CPU/estimated  |  time/estimated | play/CPU |   ETA\n");
  115.     return;
  116.   }  
  117.  
  118.   ts_calc_times(&real_time, samp_rate, frameNum, totalframes, framesize);
  119.   ts_calc_times(&process_time, samp_rate, frameNum, totalframes, framesize);
  120.  
  121.   if (totalframes > 1) {
  122.     percent = (int)(100.0 * frameNum / (totalframes - 1));
  123.   } else {
  124.     percent = 100;
  125.   }
  126.  
  127. #  define TS_TIME_DECOMPOSE(time) \
  128.     (int)((long)(time+.5) / 3600), \
  129.     (int)((long)((time+.5) / 60) % 60), \
  130.     (int)((long)(time+.5) % 60)
  131.  
  132.   fprintf(stderr,
  133.     "\r%6ld/%6ld(%3d%%)|%2d:%02d:%02d/%2d:%02d:%02d|%2d:%02d:%02d/%2d:%02d:%02d|%10.4f|%2d:%02d:%02d ",
  134.     frameNum,
  135.     totalframes - 1,
  136.     percent,
  137.     TS_TIME_DECOMPOSE(process_time.so_far),
  138.     TS_TIME_DECOMPOSE(process_time.estimated),
  139.     TS_TIME_DECOMPOSE(real_time.so_far),
  140.     TS_TIME_DECOMPOSE(real_time.estimated),
  141.     process_time.speed,
  142.     TS_TIME_DECOMPOSE(real_time.eta)
  143.   );
  144.  
  145.   fflush(stderr);
  146. }
  147.  
  148.  
  149. void timestatus_finish(void)
  150. {
  151.   fprintf(stderr, "\n");
  152.   fflush(stderr);
  153. }
  154.  
  155.  
  156.  
  157. #if (defined LIBSNDFILE || defined LAMESNDFILE)
  158.  
  159. /* these functions are used in get_audio.c */
  160.  
  161. void decoder_progress(lame_global_flags *gfp)
  162. {
  163.     fprintf(stderr,"\rFrame# %lu [ %lu]  %ikbs",gfp->frameNum,
  164.           gfp->totalframes-1,gfp->brate);
  165. }
  166.  
  167. void decoder_progress_finish(lame_global_flags *gfp)
  168. {
  169.     fprintf(stderr,"\n");
  170. }
  171.  
  172. #endif
  173.  
  174.  
  175.